home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 24 / Amiga Format AFCD24 (Feb 1998, Issue 108).iso / -in_the_mag- / emulation / amiga / uae-0.7.0b2 / src / sd-file / sound.c next >
C/C++ Source or Header  |  1998-01-20  |  3KB  |  139 lines

  1.  /* 
  2.   * UAE - The Un*x Amiga Emulator
  3.   * 
  4.   * Support for Linux/USS sound
  5.   * 
  6.   * Copyright 1997 Bernd Schmidt
  7.   */
  8.  
  9. #include "sysconfig.h"
  10. #include "sysdeps.h"
  11.  
  12. #include "config.h"
  13. #include "options.h"
  14. #include "memory.h"
  15. #include "custom.h"
  16. #include "audio.h"
  17. #include "gensound.h"
  18. #include "sounddep/sound.h"
  19. #include "events.h"
  20.  
  21. #include <sys/ioctl.h>
  22. #include <sys/soundcard.h>
  23.  
  24. int sound_fd;
  25. static int have_sound;
  26. static unsigned long formats;
  27.  
  28. unsigned long sndbuf_written;
  29.  
  30. uae_u16 sndbuffer[44100];
  31. uae_u16 *sndbufpt;
  32. int sndbufsize;
  33.  
  34. static int exact_log2(int v)
  35. {
  36.     int l = 0;
  37.     while ((v >>= 1) != 0)
  38.     l++;
  39.     return l;
  40. }
  41.  
  42. void close_sound(void)
  43. {
  44.     int t;
  45.     uae_u32 v;
  46.     char buf[4];
  47.     
  48.     if (!have_sound)
  49.     return;
  50.     
  51.     t = 0;
  52.     v = sndbuf_written;
  53.     buf[t] = v & 255;
  54.     buf[t+1] = (v>>8) & 255;
  55.     buf[t+2] = (v>>16) & 255;
  56.     buf[t+3] = (v>>24) & 255;
  57.     lseek (sound_fd, 40, SEEK_SET);
  58.     write (sound_fd, buf, 4);
  59.  
  60.     v += 36;
  61.     buf[t] = v & 255;
  62.     buf[t+1] = (v>>8) & 255;
  63.     buf[t+2] = (v>>16) & 255;
  64.     buf[t+3] = (v>>24) & 255;
  65.     lseek (sound_fd, 4, SEEK_SET);
  66.     write (sound_fd, buf, 4);
  67.  
  68.     close(sound_fd);
  69. }
  70.  
  71. int setup_sound(void)
  72. {
  73.     sound_fd = open ("sound.output", O_CREAT|O_TRUNC|O_WRONLY, 0666);
  74.     have_sound = !(sound_fd < 0);
  75.     if (!have_sound)
  76.     return 0;
  77.  
  78.     sound_available = 1;
  79.     return 1;
  80. }
  81.  
  82. int init_sound (void)
  83. {
  84.     int t;
  85.     uae_u32 v;
  86.     int tmp;
  87.  
  88.     char buf[200] = "RIFF    WAVEfmt                     data    ";
  89.  
  90.     /* Prepare a .WAV header */
  91.     sndbuf_written = 44;
  92.     t = 16; v = 16;
  93.     buf[t] = v & 255;
  94.     buf[t+1] = (v>>8) & 255;
  95.     buf[t+2] = (v>>16) & 255;
  96.     buf[t+3] = (v>>24) & 255;
  97.  
  98.     t = 20; v = 0x00010001 + (currprefs.stereo ? 0x10000 : 0);
  99.     buf[t] = v & 255;
  100.     buf[t+1] = (v>>8) & 255;
  101.     buf[t+2] = (v>>16) & 255;
  102.     buf[t+3] = (v>>24) & 255;
  103.  
  104.     t = 24; v = currprefs.sound_freq;
  105.     buf[t] = v & 255;
  106.     buf[t+1] = (v>>8) & 255;
  107.     buf[t+2] = (v>>16) & 255;
  108.     buf[t+3] = (v>>24) & 255;
  109.     t = 32; v = ((currprefs.sound_bits == 8 ? 1 : 2)
  110.          * (currprefs.stereo ? 2 : 1)) + 65536*currprefs.sound_bits;
  111.     buf[t] = v & 255;
  112.     buf[t+1] = (v>>8) & 255;
  113.     buf[t+2] = (v>>16) & 255;
  114.     buf[t+3] = (v>>24) & 255;
  115.     t = 28; v = (currprefs.sound_freq * (currprefs.sound_bits == 8 ? 1 : 2)
  116.          * (currprefs.stereo ? 2 : 1));
  117.     buf[t] = v & 255;
  118.     buf[t+1] = (v>>8) & 255;
  119.     buf[t+2] = (v>>16) & 255;
  120.     buf[t+3] = (v>>24) & 255;
  121.     write (sound_fd, buf, 44);
  122.  
  123.     sample_evtime = (long)maxhpos * maxvpos * 50 / currprefs.sound_freq;
  124.  
  125.     if (currprefs.sound_bits == 16) {
  126.     init_sound_table16 ();
  127.     eventtab[ev_sample].handler = currprefs.stereo ? sample16s_handler : sample16_handler;
  128.     } else {
  129.     init_sound_table8 ();
  130.     eventtab[ev_sample].handler = currprefs.stereo ? sample8s_handler : sample8_handler;
  131.     }
  132.     sound_available = 1;
  133.     sndbufsize = 44100;
  134.     printf ("Writing sound into \"sound.output\"; %d bits at %d Hz\n",
  135.         currprefs.sound_bits, currprefs.sound_freq, sndbufsize);
  136.     sndbufpt = sndbuffer;
  137.     return 1;
  138. }
  139.